import { createStore } from 'vuex'
import {
    login,
    logout,
} from '@/api/user'

import { useRoute, useRouter } from "vue-router";
const router = useRouter();
import {
    getToken,
    setToken,
    removeToken,
    setUserInfo,
    setRolesMes, setUserUnitName
} from '@/utils/auth'
const store = createStore({
    state: {
        token: getToken(), //!存储token
        stateCode: 0,
        iframeUrl: '',
        isAuto: false,
        refresh: false,
        userInfo: '',
        loginResInfo: '', //用户信息
        timeType: '',//右上角的时间选择
    },
    getters: {

    },
    mutations: {
        setStateCode(state, code: number) {
            state.stateCode = code
            console.log('stateCode', state.stateCode)
        },
        setIframeUrl(state, url: string) {
            state.iframeUrl = url
        },
        setIsAuto(state, isAuto: boolean) {
            state.isAuto = isAuto
        },
        setRefresh(state) {
            state.refresh = !state.refresh
        },
        USER_INFO: (state, val) => {
            state.userInfo = val
        },
        LOGIN_RES_INFO: (state, val) => {
            state.loginResInfo = val
        },
        TABLE_DATE: (state, val) => {
            state.timeType = val
        },
        SET_TOKEN: (state, token) => {
            state.token = token
        },
    },
    actions: {
        login({
            commit
        }, userInfo) {
            const {
                username,
                password,
                captcha,
                checkKey
            } = userInfo;
            commit('USER_INFO', checkKey)

            return new Promise((resolve, reject) => {
                login({
                    username: username.trim(),
                    password: password,
                    captcha: captcha.trim(),
                    checkKey: checkKey.trim()
                }).then(response => {
                    console.log('登录', response, response?.data, response?.data.code);

                    if (response?.data.code != 200) {
                        return reject(response)
                    }
                    const {
                        result
                    } = response.data
                    console.log('登录信息', result);

                    //! 设置token
                    commit('SET_TOKEN', result.token)
                    //!设置token
                    setToken(result.token)
                    localStorage.setItem('token', result.token)
                    // localStorage.setItem('userInfo', JSON.stringify(result.userInfo))
                    setUserUnitName(result.unitName)
                    setUserInfo(JSON.stringify(result.userInfo))
                    commit('LOGIN_RES_INFO', JSON.stringify(result.userInfo))
                    if (result?.roles && result?.roles.length) {
                        let roles = result.roles.join(',')
                        setRolesMes(roles)
                    } else {
                        setRolesMes('')
                    }
                    resolve(response.data)
                }).catch(error => {
                    reject(error)
                })
            })

        },
        // 用户退出登录
        logout({
            commit,
            state
        }) {
            return new Promise((resolve, reject) => {
                logout().then(() => {
                    removeToken() // must remove  token  first 
                    commit('RESET_STATE')
                    resolve(true)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // remove token
        resetToken({
            commit
        }) {
            return new Promise(resolve => {
                removeToken() // must remove  token  first
                commit('RESET_STATE')
                resolve(true)
            })
        },
    }
})

export default store
